<template>
  <div class="sellAfters">
    <el-dialog
      title="售后申请"
      :visible.sync="visible"
      width="800px"
      modal-append-to-body
      center
      @close="onClose"
    >
      <el-form
        v-if="visible"
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
      >
        <el-form-item label="关联订单" prop="serviceOrderId">
          <span>{{ ruleForm.serviceOrderId }}</span>
        </el-form-item>

        <el-form-item label="售后类型" prop="afterSaleType">
          <el-select v-model="ruleForm.afterSaleType" placeholder="请选择">
            <el-option
              v-for="item in reasonList"
              :key="item.dictLabel"
              :label="item.dictLabel"
              :value="item.dictLabel"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="问题描述" prop="problem">
          <el-input
            type="textarea"
            v-model="ruleForm.problem"
            :maxlength="100"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="ruleForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="证据信息" prop="proof">
          <TheUpload
            ref="proofRefs"
            :files="ruleForm['proof']"
            :limit="9"
            type="text"
            accept=".jpg,.png,.jpeg,.mp4,.mp3"
            @upload="
              (e) => (ruleForm['proof'] = e.files.map((file) => file.url))
            "
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="info" @click="onClose">取消</el-button>
        <el-button type="primary" @click="submit">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import TheUpload from "@/components/TheUpload";
import { validatePhone } from "@/utils/validate";
import { apiDictSaleType } from "@/api/order.js";
export default {
  components: { TheUpload },
  props: {
    serviceOrderId: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      visible: false,
      ruleForm: {
        serviceOrderId: this.serviceOrderId,
        afterSaleType: "",
        problem: "",
        phone: "",
        proof: [],
      },
      reasonList: [],
      rules: {
        afterSaleType: [
          { required: true, message: "请选择售后类型", trigger: "blur" },
        ],
        problem: [
          { required: true, message: "请填写问题描述", trigger: "blur" },
        ],
        phone: [
          { required: true, message: "请填写联系方式", trigger: "blur" },
          { validator: validatePhone, trigger: "blur" },
        ],
        proof: [
          {
            type: "array",
            required: true,
            message: "请上传证据信息",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      apiDictSaleType().then((res) => {
        this.reasonList = res.data;
      });
    },
    onShow() {
      this.ruleForm.serviceOrderId = this.serviceOrderId;
      this.visible = true;
    },
    onClose() {
      this.$refs.ruleForm.resetFields();
      this.visible = false;
    },

    submit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          console.log(this.ruleForm, "售后");
          this.$emit("sendSellAfter", this.ruleForm);
          this.$nextTick(() => {
            this.onClose();
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
